<script>
export default {
  name: "Login",
};
</script>
<script setup>
import Toast, { toastEffect } from "@/components/Toast";
import { instanceRQ, post } from "@/API/axios.js";
import { useRouter } from "vue-router";
import { ref, reactive } from "vue";
const router = useRouter();
const loginData = reactive({
  username: "",
  password: "",
});

//toast相关
const { toastData, showToast } = toastEffect();

const handleLogin = async () => {
  if (!loginData.password || !loginData.username) {
    showToast("请输入完整用户名称及密码", 1500);
    return;
  }
  try {
    const res = await post("/api/user/login", {
      username: loginData.username,
      password: loginData.password,
    });
    if (res.errno === 0) {
      localStorage.isLogin = true;
      router.push({ name: "home" });
      showToast("登录成功", 500);
    } else {
      showToast("登录错误", 2000);
    }
  } catch (error) {
    showToast(error, 2000);
  }
};

const handlerRegister = () => {
  console.log("gogo");
  router.push({ name: "register" });
};
</script>

<template>
  <div class="wrapper">
    <div class="portrait">
      <img class="portrait__img" src="@/assets/navicons/edit.png" alt="" />
    </div>
    <div class="inp">
      <input
        class="inp__val"
        type="text"
        placeholder-class="plc"
        placeholder="请输入用户名"
        v-model="loginData.username"
      />
    </div>
    <div class="inp">
      <input
        class="inp__val"
        type="password"
        placeholder-class="plc"
        placeholder="请输入密码"
        v-model="loginData.password"
      />
    </div>
    <div class="btn" @click="handleLogin">登录</div>
    <div class="addInfo">
      <p class="regi" @click="handlerRegister">立即注册</p>
      <p class="regi">忘记密码</p>
    </div>
  </div>
  <Toast v-if="toastData.isShow" :msg="toastData.msg" />
</template>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.plc {
  font-size: 0.16rem;
  color: $plc-color;
  letter-spacing: 0;
  line-height: 0.24rem;
}
.wrapper {
  width: 100%;
  margin: 0 auto;
}
.portrait {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 1.2rem;
  margin-bottom: 0.4rem;
  &__img {
    height: 0.66rem;
    width: 0.66rem;
  }
}

.inp {
  width: 2.95rem;
  height: 0.48rem;
  background-color: #f9f9f9;
  margin: 0 auto 0.16rem auto;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 0.06rem;
  &__val {
    box-sizing: border-box;
    height: 100%;
    width: 100%;
    background: none;
    border: none;
    padding: 0.05rem 0.16rem;
    font-size: 0.16rem;
    color: $plc-color;
    letter-spacing: 0;
    line-height: 0.22rem;
  }
}

.btn {
  width: 2.95rem;
  height: 0.48rem;
  margin: 0.32rem auto 0.16rem auto;
  background: #0091ff;
  box-shadow: 0 4px 8px 0 rgba(0, 145, 255, 0.32);
  border-radius: 0.04rem;
  color: #fff;
  font-size: 0.16rem;
  letter-spacing: 0;
  text-align: center;
  line-height: 0.48rem;
}

.addInfo {
  height: 0.2rem;
  width: 100%;
  display: flex;
  justify-content: center;
  .regi {
    font-size: 0.14rem;
    color: rgba(0, 0, 0, 0.5);
    letter-spacing: 0;
  }
  .regi:first-child {
    border-right: 1px solid rgba(0, 0, 0, 0.5);
    padding-right: 0.14rem;
    margin-right: 0.14rem;
  }
}
</style>
